﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BHXH.Core.Domain;
using BHXH.Core.IService;
using Infragistics.Win.UltraWinGrid;
using FX.Core;
using Infragistics.Win;


namespace BHXH.Frm.FrmUserControl
{
    public partial class D07_TS : UserControl
    {
        private readonly Ihs_D07TS_dtService _hs_d07tsSrv;
        public IList<string> ID_D07TS = new List<string>();
        private IList<string> _List_ID_Xoa;
        bool Xoa_hs_h07ts = false;
        bool update_hs_d07ts = false;
        bool add_hs_d07ts = false;

        public IList<string> List_ID_Xoa
        {
            get { return _List_ID_Xoa; }
            set { _List_ID_Xoa = value; }
        }
        int Stt = 1;
        public D07_TS()
        {
            InitializeComponent();
            _hs_d07tsSrv = IoC.Resolve<Ihs_D07TS_dtService>();
            LoadGridview_D07_TS();
        }

        public void LoadGridview_D07_TS()
        {
            uG_DanhSachDeNghiThayDoiTTBH.DataSource = GetAll_D02bTS_Tab();
            uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Override.ActiveCellAppearance.Reset();
            uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Override.ActiveRowAppearance.Reset();
            this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0].RowLayoutStyle = RowLayoutStyle.ColumnLayout;
            this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0].Override.AllowRowLayoutLabelSizing = RowLayoutSizing.Both;
            this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0].Columns["ID"].Hidden = true;
            this.uG_DanhSachDeNghiThayDoiTTBH.SyncWithCurrencyManager = false;

            this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Override.SelectTypeRow = SelectType.Single;
            this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0].Override.SelectTypeRow = SelectType.Extended;
            this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0].Override.MaxSelectedRows = 100;
        }

        public DataTable GetAll_D02bTS_Tab()
        {
            IList<hs_D07TS_dt> a = _hs_d07tsSrv.GetAll_hs_d07tsdt();
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("STT", typeof(string));
            table.Columns.Add("Họ và tên", typeof(string));
            table.Columns.Add("Mã số BHXH", typeof(string));           
            table.Columns.Add("Nội Dung Đề Nghị Thay Đổi(Điều Chỉnh)", typeof(string));
            table.Columns.Add("Cũ", typeof(string));
            table.Columns.Add("Mới", typeof(string));
            table.Columns.Add("Từ Tháng, Năm", typeof(string));
            table.Columns.Add("Đến Tháng, Năm", typeof(string));
            table.Columns.Add("Căn Cứ Điều Chỉnh", typeof(string));  

            if (a.Count > 0)
            {
                foreach (var item in a)
                {
                    table.Rows.Add(item.ID,Stt, item.HoTen, item.MaSoBHXH, item.NoiDung, item.Cu, item.Moi, item.TuThangNam
                        , item.DenThangNam, item.CanCuDieuChinh);
                    Stt += 1;
                }
            }
            else
            {
                table.Rows.Add(null, Stt, null, null, null, null, null, null, null, null);
                Stt += 1;
            }
            return table;
        }

        private void uG_DanhSachDeNghiThayDoiTTBH_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F12:
                    UltraGridRow row = this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0].AddNew();
                    row.Cells[1].Value = Stt;
                    row.Cells[2].Value = null;
                    row.Cells[3].Value = null;
                    row.Cells[4].Value = null;
                    row.Cells[5].Value = null;
                    row.Cells[6].Value = null;
                    row.Cells[7].Value = null;
                    row.Cells[8].Value = null;
                    row.Cells[9].Value = null;
                   
                    Stt += 1;
                    break;
                case Keys.F11:
                    if (this.uG_DanhSachDeNghiThayDoiTTBH.Selected.Rows.Count > 0)
                    {
                        ID_D07TS.Add(this.uG_DanhSachDeNghiThayDoiTTBH.ActiveRow.Cells["ID"].Value.ToString());
                        this.uG_DanhSachDeNghiThayDoiTTBH.DeleteSelectedRows();
                        List_ID_Xoa = ID_D07TS;
                        Stt -= 1;
                    }
                    else
                    {
                        MessageBox.Show("Chọn dòng để xóa.");
                    }                                
                    break;
            }
        }       

        public void Xoa_D02bTS()
        {
            
            if (DialogResult.Yes == MessageBox.Show("Bạn có chắc chắn xóa không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                foreach (var item in ID_D07TS)
                {
                    if (_hs_d07tsSrv.Delete_hs_d07tsdt(item) > 0)
                    {
                        Xoa_hs_h07ts = true;
                    }
                }                
            }
        }

        public void Ghi_D02bTS()
        {
            string ID = "";
            hs_D07TS_dt hs = new hs_D07TS_dt();
            UltraGridBand band = this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0];
            foreach (UltraGridRow row in band.GetRowEnumerator(GridRowType.DataRow))
            {                
                UltraGridColumn column = this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0].Columns[0];
                ID = row.Cells[column].Value.ToString();
                hs.HoTen = row.Cells[this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0].Columns[2]].Value.ToString();
                hs.MaSoBHXH = row.Cells[this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0].Columns[3]].Value.ToString();
                hs.NoiDung = row.Cells[this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0].Columns[4]].Value.ToString();
                hs.Cu = row.Cells[this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0].Columns[5]].Value.ToString();
                hs.Moi = row.Cells[this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0].Columns[6]].Value.ToString();
                hs.TuThangNam = row.Cells[this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0].Columns[7]].Value.ToString();
                hs.DenThangNam = row.Cells[this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0].Columns[8]].Value.ToString();
                hs.CanCuDieuChinh = row.Cells[this.uG_DanhSachDeNghiThayDoiTTBH.DisplayLayout.Bands[0].Columns[9]].Value.ToString();
                if (hs.HoTen.ToString() == "" || hs.MaSoBHXH.ToString() == "" || hs.MaSoBHXH.ToString() == "" || hs.Cu.ToString() == "" || hs.Moi.ToString() == "" || hs.TuThangNam.ToString() == "" || hs.DenThangNam.ToString() == "" || hs.CanCuDieuChinh.ToString() == "")
                {
                    MessageBox.Show(MainResource.msgDuLieuTrong, MainResource.textCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    if (!ID.Equals(""))
                    {
                        hs.ID = int.Parse(ID);
                        _hs_d07tsSrv.Update_hs_d07tsdt(hs);
                        update_hs_d07ts = true;
                    }
                    else
                    {
                        if (_hs_d07tsSrv.Add_hs_d07tsdt(hs) == true)
                        {
                            add_hs_d07ts = true;
                        }
                    }
                }
            }           
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            Ghi_D02bTS();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            Xoa_D02bTS();
        }       
        
    }
}
